/**
 * 喵绅士
 * https://zha.nyabus.com/tag/snuff/chinese
 */
package com.zsj;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class mytest1 {
    public static void main(String[] args) throws Exception {
        String url1 = "https://zha.nyabus.com/g/347851/";
        Document document = Jsoup.connect(url1).get();
        Elements elements1 = document.select("#info h1");

        String url2 = url1+"list/1";
        Document document2 = Jsoup.connect(url2).get();
        Element yeshu = document2.select(".num-pages").first();
        String pa=yeshu.text();
        int page = Integer.parseInt(pa);
        String alt = elements1.text();
        System.out.println(alt);
        for(int i=1;i<=page;i++){
            String url4 = url1+"list/"+i;
            Document document4 = Jsoup.connect(url4).get();
            Elements select = document4.select("#image-container a img");
            String dizhi=select.attr("src");
            System.out.println(dizhi);
            //使用这个地址下载图片
            //1.使用Java代码模拟出一个客户端
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //2.创建一个get请求
            HttpGet httpGet = new HttpGet(dizhi);
            //配置请求信息
//                RequestConfig config = RequestConfig.custom().setConnectTimeout(10000)   //创建连接的最长时间，单位是毫秒
//                        .setConnectionRequestTimeout(10000)   //设置获取连接的最长时间，单位是毫秒
//                        .setSocketTimeout(20*1000)      //设置数据传输的最长时间，单位是毫秒
//                        .build();
//
//                //给请求设置请求信息
//                httpGet.setConfig(config);

            //添加头部信息模拟浏览器访问
            httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            httpGet.setHeader("Accept-Encoding", "gzip, deflate, sdch, br");
            httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.8");
            httpGet.setHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
            //    httpGet.setHeader("Referer", "https://zha.nyabus.com/");//告诉服务器women从哪里来的
            //3.使用客户端执行请求,获取响应
            CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
            //4.获取响应体
            HttpEntity entity = httpResponse.getEntity();
            //5.获取响应体的内容
            InputStream is = entity.getContent();
            System.out.println(is);
            //创建一个字节输出流，将图片输出到硬盘中"D/aa"目录
            //解析src获取图片的后缀名
            //int i1 = src.lastIndexOf(".");//得到的是最后一个 . 的索引,然后用substring来根据索引切割
            String sub = dizhi.substring(dizhi.lastIndexOf("."));
            //创建一个随时间毫秒值变化的的文件名
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHHmmssSSS");
//                String imgName = sdf.format(date) + sub;
            String imgName = i + sub;
            File dir = new File("E:/date/零食/" + alt);
            if (!dir.exists()) {// 判断目录是否存在
                dir.mkdirs();
            }
            FileOutputStream out = new FileOutputStream(dir + "/" + imgName);//具体放在哪个地方可以由你自己确定,但是记得要这个文件夹一定要存在,否则会报错
            //将输入流中的内容拷贝到输出流
            IOUtils.copy(is, out);
            //关流
            out.close();
            is.close();

//                OutputStream outputStream = new FileOutputStream(new File("E:\\date\\零食\\" +alt+"\\"+imgName));
//                httpResponse.getEntity().writeTo(outputStream);

            System.out.println("第"+i+"页下载完成.......");

            Toolkit.getDefaultToolkit().beep();
        }
        System.out.println("本次下载完成了,快去打开吧...");
        Toolkit.getDefaultToolkit().beep();
    }
}